//
//  NearbyViewController.m
//  theCoupleLive
//
//  Created by USER on 2016/12/4.
//  Copyright © 2016年 USER. All rights reserved.
//

#import "NearbyViewController.h"
#import "groupViewController.h"
#import "humanViewController.h"
#import "dynamicViewController.h"

@interface NearbyViewController ()<UIScrollViewDelegate>
@property (strong, nonatomic) UIScrollView *scrollview;
@property (strong,nonatomic)UIView *topview;//顶部的view
@property (strong,nonatomic)groupViewController *groupVC;
@property (strong,nonatomic)humanViewController *humanVC;
@property (strong,nonatomic)dynamicViewController *dynamicVC;
@property (assign,nonatomic)int page;
@property (strong,nonatomic)UIButton *btn_select;
@property (strong,nonatomic)UIButton *btn_dynamic;
@property (strong,nonatomic)UIButton *btn_human;
@property (strong,nonatomic)UIButton *btn_group;
@end

@implementation NearbyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden=YES;
    self.automaticallyAdjustsScrollViewInsets=NO;
    //创建导航栏的view
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.topview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen]bounds].size.width, 64)];
    self.topview.backgroundColor=[UIColor colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1];
    [self.view addSubview:self.topview];
    
    
    //创建scrollview
    self.scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];
    [self.view addSubview:self.scrollview];
    
    //动态
    self.dynamicVC=[[dynamicViewController alloc]init];
    [self addChildViewController:self.dynamicVC];
    self.dynamicVC.view.frame=CGRectMake(0, 0, self.scrollview.frame.size.width, self.scrollview.frame.size.height-49);
    [self.scrollview addSubview:self.dynamicVC.view];
    
    //人
    self.humanVC=[[humanViewController alloc]init];
    [self addChildViewController:self.humanVC];
    self.humanVC.view.frame=CGRectMake(self.scrollview.frame.size.width, 0, self.scrollview.frame.size.width, self.scrollview.frame.size.height-49);
    [self.scrollview addSubview:self.humanVC.view];
    
    //群组
    self.groupVC=[[groupViewController alloc]init];
    [self addChildViewController:self.groupVC];
    self.groupVC.view.frame=CGRectMake(2*self.scrollview.frame.size.width, 0, self.scrollview.frame.size.width, self.scrollview.frame.size.height-49);
    [self.scrollview addSubview:self.groupVC.view];
    
    //设置scrollview属性
    self.scrollview.contentSize=CGSizeMake(3*self.scrollview.frame.size.width, self.scrollview.frame.size.height);
    self.scrollview.showsHorizontalScrollIndicator=FALSE;
    self.scrollview.showsVerticalScrollIndicator=FALSE;
    self.scrollview.pagingEnabled=YES;
    self.scrollview.bounces=NO;
    self.scrollview.delegate=self;
    
    //右上方的按钮
    self.btn_select=[[UIButton alloc]initWithFrame:CGRectMake([[UIScreen mainScreen]bounds].size.width-55*widthScale, 20*heightScale, 55*widthScale, 44*heightScale)];
    [self.btn_select addTarget:self action:@selector(action1) forControlEvents:UIControlEventTouchUpInside];
    [self.topview addSubview:self.btn_select];
    
    //创建顶部的三个button
    NSArray *segData=[[NSArray alloc]initWithObjects:@"动态",@"人",@"群组", nil];
    //动态
    self.btn_dynamic=[[UIButton alloc]initWithFrame:CGRectMake(30*widthScale, 30*heightScale, 70*widthScale, 25*heightScale)];
    [self.btn_dynamic setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btn_dynamic setBackgroundColor:[UIColor whiteColor]];
    self.btn_dynamic.layer.masksToBounds=YES;
    self.btn_dynamic.layer.cornerRadius=12.5;
    self.btn_dynamic.tag=100;
    [self.btn_dynamic addTarget:self action:@selector(selectView:) forControlEvents:UIControlEventTouchUpInside];
    [self.btn_dynamic setTitle:[segData objectAtIndex:0] forState:UIControlStateNormal];
    self.btn_dynamic.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    
    //人
    self.btn_human=[[UIButton alloc]initWithFrame:CGRectMake(self.btn_dynamic.frame.size.width+self.btn_dynamic.frame.origin.x, 30*heightScale, 70*widthScale, 25*heightScale)];
    [self.btn_human setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btn_human setBackgroundColor:[UIColor colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1]];
    self.btn_human.layer.masksToBounds=YES;
    self.btn_human.layer.cornerRadius=12.5;
    self.btn_human.tag=101;
    [self.btn_human addTarget:self action:@selector(selectView:) forControlEvents:UIControlEventTouchUpInside];
    [self.btn_human setTitle:[segData objectAtIndex:1] forState:UIControlStateNormal];
    self.btn_human.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    
    //群组
    self.btn_group=[[UIButton alloc]initWithFrame:CGRectMake(self.btn_human.frame.size.width+self.btn_human.frame.origin.x, 30*heightScale, 70*widthScale, 25*heightScale)];
    [self.btn_group setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.btn_group setBackgroundColor:[UIColor colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1]];
    self.btn_group.layer.masksToBounds=YES;
    self.btn_group.layer.cornerRadius=12.5;
    self.btn_group.tag=102;
    [self.btn_group addTarget:self action:@selector(selectView:) forControlEvents:UIControlEventTouchUpInside];
    [self.btn_group setTitle:[segData objectAtIndex:2] forState:UIControlStateNormal];
    self.btn_group.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    
    [self.topview addSubview:self.btn_dynamic];
    [self.topview addSubview:self.btn_human];
    [self.topview addSubview:self.btn_group];
    self.btn_dynamic.selected=YES;
    [self.btn_dynamic setTitleColor:[UIColor  colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1] forState:UIControlStateSelected];
    [self.btn_human setTitleColor:[UIColor colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1] forState:UIControlStateSelected];
    [self.btn_group setTitleColor:[UIColor  colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1] forState:UIControlStateSelected];
    
    //初始化在界面二人的界面
    self.page=1;
    CGFloat offsetX = self.page * self.scrollview.frame.size.width;
    CGPoint offset = CGPointMake(offsetX, 0);
    [self.scrollview setContentOffset:offset animated:YES];
    // Do any additional setup after loading the view.
}

-(void)selectView:(id)sender{
    
    UIButton *button=(UIButton*)sender;
    
    NSInteger tag1=button.tag-100;
    CGFloat offsetX = tag1 * self.scrollview.frame.size.width;
    CGPoint offset = CGPointMake(offsetX, 0);
    [self.scrollview setContentOffset:offset animated:YES];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat w=self.scrollview.frame.size.width;
    self.page=(self.scrollview.contentOffset.x+w*0.5)/w;
    
    self.navigationItem.rightBarButtonItem=nil;
    if (self.page==0) {
        self.btn_dynamic.selected=YES;
        self.btn_human.selected=NO;
        self.btn_group.selected=NO;
        self.btn_dynamic.backgroundColor=[UIColor whiteColor];
        self.btn_human.backgroundColor=[UIColor colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1];
        self.btn_group.backgroundColor=[UIColor colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1];
        self.btn_select.hidden=YES;
//        [self.button setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
        
    }
    else if (self.page==1) {
        self.btn_dynamic.selected=NO;
        self.btn_human.selected=YES;
        self.btn_group.selected=NO;
        self.btn_dynamic.backgroundColor=[UIColor colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1];
        self.btn_human.backgroundColor=[UIColor whiteColor];
        self.btn_group.backgroundColor=[UIColor colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1];
        self.btn_select.hidden=NO;
        [self.btn_select setImage:[UIImage imageNamed:@"shuaixuan"] forState:UIControlStateNormal];
    }
    else if (self.page==2){
        self.btn_dynamic.selected=NO;
        self.btn_human.selected=NO;
        self.btn_group.selected=YES;
        self.btn_dynamic.backgroundColor=[UIColor colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1];
        self.btn_human.backgroundColor=[UIColor colorWithRed:252.0/255 green:72.0/255 blue:113.0/255 alpha:1];
        self.btn_group.backgroundColor=[UIColor whiteColor];
        self.btn_select.hidden=NO;
        [self.btn_select setImage:[UIImage imageNamed:@"xinjianqunzu"] forState:UIControlStateNormal];
    }
}

-(void)action1{
    NSLog(@"%ld", (long)self.page);
    if (self.page==0) {
        
        
//        [self.navigationController setNavigationBarHidden:NO animated:YES];
//        publishDongTaiViewController *publishVC = [[publishDongTaiViewController alloc]init];
//        self.button.hidden = YES;
//        self.navigationItem.backBarButtonItem.tintColor = [UIColor colorWithRed:59.0/255.0 green:85.0/255.0 blue:251.0/255.0 alpha:0.6];
//        self.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:publishVC animated:YES];
//        self.hidesBottomBarWhenPushed = NO;
        
    }
    else if (self.page==1) {
//        selectViewController *sc=[[selectViewController alloc]initWithNibName:@"selectViewController" bundle:[NSBundle mainBundle]];
//        [self presentViewController:sc animated:YES completion:nil];
    }
    else {
//        createQunZuViewController *cc=[[createQunZuViewController alloc]initWithNibName:@"createQunZuViewController" bundle:[NSBundle mainBundle]];
//        UINavigationController *ccNa = [[UINavigationController alloc]initWithRootViewController:cc];
//        [self presentViewController:ccNa animated:YES completion:nil];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
